#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
#include "sys/file.h"
#include "utils/to_ostream.h"
using namespace tdl;

struct select_file_type_policy
{
	bool operator()(const tdl::file_info& fi, void* p)
	{
		string& fn_tpl = *(string*)p;

		return (fi.type == tdl::FILE_TYPE_COMM 
			&& fi.name.c_str()[0] != '.'
			&& (int)fi.name.find(fn_tpl) >= 0);
	}
};

//////////////////////
int main(int argc, char* argv[])
{
	string fn(argv[1]);
	tdl::directory dir;
	vector<file_info> deis;

	try {
		tdl::select_file_all selor;
		dir.read<tdl::sort_fileinfo_policy_by_name>(deis, selor);
	} catch(tdl::file_ex& e) {
		cerr << e.what_hell() << endl;
	}

	try {
		tdl::file_info fi;
		if(! tdl::get_file_info(fn, fi))
		{
			cerr << "no " << fn << " exists" << endl;
			return 1;
		}
		else
		{
			cout << "path=" << fi.path << endl;
			cout << "name=" << fi.name << endl;
		}

		dir.open(fn);
		string fn_tpl = ".cpp";
		select_file_type_policy selor;
		dir.read<tdl::sort_fileinfo_policy_by_name>(deis, selor, &fn_tpl);
		dir.close();

		cout << deis << endl;
	} catch(tdl::file_ex& e) {
		cerr << e.what_hell() << endl;
	}

	return 0;
}

